package ynu.edu.merchant.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.merchant.dto.MerchantReviewDTO;
import ynu.edu.merchant.service.MerchantReviewService;
import ynu.edu.merchant.vo.MerchantReviewVO;

import java.math.BigDecimal;

/**
 * 商家评价控制器
 */
@RestController
@RequestMapping("/merchant-reviews")
@RequiredArgsConstructor
public class MerchantReviewController {

    private final MerchantReviewService reviewService;

    /**
     * 创建商家评价
     */
    @PostMapping
    public Result<MerchantReviewVO> createReview(@RequestBody @Valid MerchantReviewDTO reviewDTO) {
        MerchantReviewVO reviewVO = reviewService.createReview(reviewDTO);
        return Result.success(reviewVO);
    }

    /**
     * 更新商家评价
     */
    @PutMapping("/{id}")
    public Result<MerchantReviewVO> updateReview(@PathVariable Long id, @RequestBody @Valid MerchantReviewDTO reviewDTO) {
        reviewDTO.setId(id);
        MerchantReviewVO reviewVO = reviewService.updateReview(reviewDTO);
        return Result.success(reviewVO);
    }

    /**
     * 获取商家评价信息
     */
    @GetMapping("/{id}")
    public Result<MerchantReviewVO> getReviewById(@PathVariable Long id) {
        MerchantReviewVO reviewVO = reviewService.getReviewById(id);
        return Result.success(reviewVO);
    }

    /**
     * 删除商家评价
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteReview(@PathVariable Long id) {
        boolean result = reviewService.deleteReview(id);
        return Result.success(result);
    }

    /**
     * 根据商家ID查询评价列表
     */
    @GetMapping("/merchant/{merchantId}")
    public Result<Page<MerchantReviewVO>> getReviewsByMerchantId(
            @PathVariable Long merchantId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<MerchantReviewVO> reviewPage = reviewService.getReviewsByMerchantId(merchantId, page, size);
        return Result.success(reviewPage);
    }

    /**
     * 根据用户ID查询评价列表
     */
    @GetMapping("/user/{userId}")
    public Result<Page<MerchantReviewVO>> getReviewsByUserId(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<MerchantReviewVO> reviewPage = reviewService.getReviewsByUserId(userId, page, size);
        return Result.success(reviewPage);
    }

    /**
     * 商家回复评价
     */
    @PutMapping("/{id}/reply")
    public Result<Boolean> replyReview(
            @PathVariable Long id,
            @RequestParam String reply) {
        boolean result = reviewService.replyReview(id, reply);
        return Result.success(result);
    }

    /**
     * 更新评价状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateReviewStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        boolean result = reviewService.updateReviewStatus(id, status);
        return Result.success(result);
    }

    /**
     * 计算商家的平均评分
     */
    @GetMapping("/merchant/{merchantId}/rating")
    public Result<BigDecimal> calculateAverageRating(@PathVariable Long merchantId) {
        BigDecimal avgRating = reviewService.calculateAverageRating(merchantId);
        return Result.success(avgRating);
    }

    /**
     * 统计商家的评价数量
     */
    @GetMapping("/merchant/{merchantId}/count")
    public Result<Long> countReviewsByMerchantId(@PathVariable Long merchantId) {
        long count = reviewService.countReviewsByMerchantId(merchantId);
        return Result.success(count);
    }
} 